﻿using System;
using System.Collections.Specialized;
using System.Globalization;
using System.Web;
using Peter.Common;
using Peter.Sdw.Data;
using Peter.Sdw.Properties;

namespace Peter.Sdw.Logic
{
   /// <summary>
   /// Data object for a footer.
   /// </summary>
   public class FooterModel : ViewModelBase, IStyleable
   {
      private string m_Text;
      private readonly LinkModel m_LinkModel;
      private readonly StyleModel m_StyleModel;

      /// <summary>
      /// Initializes a new footer.
      /// </summary>
      public FooterModel (FooterListModel listModel)
      {
         this.ListModel = listModel;
         this.m_LinkModel = new LinkModel (this);
         this.m_StyleModel = new StyleModel (this);
      }

      /// <summary>
      /// Get the parent footer list.
      /// </summary>
      public FooterListModel ListModel { get; private set; }

      /// <summary>
      /// Gets or Sets the text of the footer.
      /// </summary>
      public string Text
      {
         get { return this.m_Text; }
         set
         {
            this.m_Text = value;
            this.RaisePropertyChanged ("Text");
         }
      }

      /// <summary>
      /// Gets the start index.
      /// </summary>
      public int StartIndex
      {
         get { return this.ListModel.StartIndex; }
         set { this.ListModel.StartIndex = value; }
      }

      /// <summary>
      /// Gets the end index.
      /// </summary>
      public int EndIndex
      {
         get { return this.ListModel.EndIndex; }
         set { this.ListModel.EndIndex = value; }
      }

      /// <summary>
      /// Gets the style input model.
      /// </summary>
      public LinkModel LinkInput { get { return this.m_LinkModel; } }

      /// <summary>
      /// Gets the style input model.
      /// </summary>
      public StyleModel StyleInput { get { return this.m_StyleModel; } }

      /// <summary>
      /// Adds the given style.
      /// </summary>
      /// <param name="style">The style to add.</param>
      public void AddStyle (Style style)
      {
         var parameters = new NameValueCollection {
               {Constants.REQUEST_TOKEN, WebQueue.Instance.RequestToken},
               {"itemId", this.ListModel.ParentId.ToString (CultureInfo.InvariantCulture)},
               {"startStyle", HttpUtility.UrlEncode (style.Start)},
               {"endStyle", HttpUtility.UrlEncode (style.End)},
               {"spansMultiple", style.SpansMultiple.ToString ()},
               {"startIndex", style.StartIndex.ToString (CultureInfo.InvariantCulture)},
               {"endIndex", style.EndIndex.ToString (CultureInfo.InvariantCulture)}
            };
         WebQueue.Instance.WebPostComplete += this.OnPostCompleted;
         WebQueue.Instance.Post (string.Format (Constants.URL_CREATE_STYLE, this.ListModel.ItemType, Constants.FOOTER), parameters);
      }

      /// <summary>
      /// Removes the given style.
      /// </summary>
      /// <param name="style">The style to remove.</param>
      public void RemoveStyle (Style style)
      {
         var parameters = new NameValueCollection {
               {Constants.REQUEST_TOKEN, WebQueue.Instance.RequestToken},
               {"id", style.Id.ToString (CultureInfo.InvariantCulture)},
               {"itemId", this.ListModel.ParentId.ToString (CultureInfo.InvariantCulture)}
            };
         WebQueue.Instance.WebPostComplete += this.OnPostCompleted;
         WebQueue.Instance.Post (string.Format (Constants.URL_DELETE_STYLE, this.ListModel.ItemType, Constants.FOOTER), parameters);
      }

      /// <summary>
      /// Posts edits for the given style.
      /// </summary>
      /// <param name="style">The style to edit.</param>
      public void EditStyle (Style style)
      {
         var parameters = new NameValueCollection {
               {Constants.REQUEST_TOKEN, WebQueue.Instance.RequestToken},
               {"id", style.Id.ToString (CultureInfo.InvariantCulture)},
               {"itemId", this.ListModel.ParentId.ToString (CultureInfo.InvariantCulture)},
               {"startStyle", HttpUtility.UrlEncode (style.Start)},
               {"endStyle", HttpUtility.UrlEncode (style.End)},
               {"spansMultiple", style.SpansMultiple.ToString ()},
               {"startIndex", style.StartIndex.ToString (CultureInfo.InvariantCulture)},
               {"endIndex", style.EndIndex.ToString (CultureInfo.InvariantCulture)}
            };
         WebQueue.Instance.WebPostComplete += this.OnPostCompleted;
         WebQueue.Instance.Post (string.Format (Constants.URL_EDIT_STYLE, this.ListModel.ItemType, Constants.FOOTER), parameters);
      }

      /// <summary>
      /// Occurs when a web post request is completed.
      /// </summary>
      /// <param name="sender">WebQueue.</param>
      /// <param name="e">WebRequestEventArgs</param>
      private void OnPostCompleted (object sender, WebRequestEventArgs e)
      {
         if (e.Url == string.Format (Constants.URL_CREATE_STYLE, this.ListModel.ItemType, Constants.FOOTER)) {
            WebQueue.Instance.WebPostComplete -= this.OnPostCompleted;
            if (e.Response.status == Constants.SUCCESS) {
               Console.WriteLine (string.Format (Resources.Data_created, Constants.FOOTER));
            }
         } else if (e.Url == string.Format (Constants.URL_DELETE_STYLE, this.ListModel.ItemType, Constants.FOOTER)) {
            WebQueue.Instance.WebPostComplete -= this.OnPostCompleted;
            if (e.Response.status == Constants.SUCCESS) {
               Console.WriteLine (string.Format (Resources.Data_deleted, Constants.FOOTER));
            }
         } else if (e.Url == string.Format (Constants.URL_EDIT_STYLE, this.ListModel.ItemType, Constants.FOOTER)) {
            WebQueue.Instance.WebPostComplete -= this.OnPostCompleted;
            if (e.Response.status == Constants.SUCCESS) {
               Console.WriteLine (Constants.HEADER + Resources.Edit_successful);
            }
         }
      }
   }
}
